<?php
namespace App\Controller;

class SysConfigsController extends AppController{

    public function initialize()
    {
        parent::initialize();
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取配置列表
     * */
    public function getList()
    {
        $title = $this->request->getData("title");
        $where = [];
        if(!empty($title)){
            $where['title like'] = '%'.$title.'%';
        }
        $sys_configs = $this->SysConfigs->find()
            ->where($where)
            ->toArray();
        $this->G->success("Success", $sys_configs);
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：新增一个配置
     * */
    public function add()
    {
        $title = $this->request->getData("title");
        $intro = $this->request->getData("intro");
        $alias = $this->request->getData("alias");
        $value = $this->request->getData("value");
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        if(empty($alias)){
            $this->G->error("alias_error");
            return null;
        }
        if(empty($value)){
            $this->G->error("value_error");
            return null;
        }
        $sysconfigs = $this->SysConfigs->newEntity();
        $sysconfigs->alias = $alias;
        $sysconfigs->title = $title;
        $sysconfigs->intro = $intro;
        $sysconfigs->value = $value;
        $this->SysConfigs->save($sysconfigs);
        $this->loadComponent('SysconfigCache');
        $this->SysconfigCache->createCache();
        $this->G->success("Success");
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：更新一个配置
     * */
    public function update()
    {
        $id = $this->request->getData("id");
        $title = $this->request->getData("title");
        $intro = $this->request->getData("intro");
        $alias = $this->request->getData("alias");
        $value = $this->request->getData("value");
        if(empty($title)){
            $this->G->error("title_error");
            return null;
        }
        if(empty($alias)){
            $this->G->error("alias_error");
            return null;
        }
        if(empty($value)){
            $this->G->error("value_error");
            return null;
        }
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $sys_configs = $this->SysConfigs->findById($id)->select(['id'])->first();
        if(empty($sys_configs)){
            $this->G->error("sys_config_error");
            return null;
        }
        $sysconfigs = $this->SysConfigs->newEntity();
        $sysconfigs->id = $id;
        $sysconfigs->alias = $alias;
        $sysconfigs->title = $title;
        $sysconfigs->intro = $intro;
        $sysconfigs->value = $value;
        $this->SysConfigs->save($sysconfigs);
        $this->loadComponent('SysconfigCache');
        $this->SysconfigCache->createCache();
        $this->G->success("Success");
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：删除一个配置
     * */
    public function delete()
    {
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $this->SysConfigs->deleteAll(['id'=>$id]);
        $this->loadComponent('SysconfigCache');
        $this->SysconfigCache->createCache();
        $this->G->success('Success');
    }

    /*
     * create by phpstorm
     * author：Ming
     * date：2020.04.27
     * ps：获取配置
     * */
    public function getInfo()
    {
        $id = $this->request->getData("id");
        if(!is_numeric($id)){
            $this->G->error("id_error");
            return null;
        }
        $sys_configs = $this->SysConfigs->findById($id)->first();
        $this->G->success('Success', $sys_configs);
    }
}
?>